package vip.xiaonuo.supplier.modular.warehouse.stocks.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.supplier.modular.base.productions.param.SupplierProductionsPageParam;
import vip.xiaonuo.supplier.modular.warehouse.stocks.entity.SupplierStocks;
import vip.xiaonuo.supplier.modular.warehouse.stocks.param.*;
import vip.xiaonuo.supplier.modular.warehouse.stocks.service.SupplierStocksService;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;

import java.io.IOException;
import java.util.List;

/**
 * 库存明细控制器
 *
 * @author Mr.wu
 * @date  2025/03/18 14:28
 */
@Tag(name = "库存明细控制器")
@RestController
@Validated
public class SupplierStocksController {

    @Resource
    private SupplierStocksService supplierStocksService;

    /**
     * 获取库存明细分页
     *
     * @author Mr.wu
     * @date  2025/03/18 14:28
     */
    @Operation(summary = "获取库存明细分页")
    @SaCheckPermission("/supplier/warehouse/stocks/page")
    @GetMapping("/supplier/warehouse/stocks/page")
    public CommonResult<Page<SupplierStocks>> page(SupplierStocksPageParam supplierStocksPageParam) {
        return CommonResult.data(supplierStocksService.page(supplierStocksPageParam));
    }

    /**
     * 添加库存明细
     *
     * @author Mr.wu
     * @date  2025/03/18 14:28
     */
    @Operation(summary = "添加库存明细")
    @CommonLog("添加库存明细")
    @SaCheckPermission("/supplier/warehouse/stocks/add")
    @PostMapping("/supplier/warehouse/stocks/add")
    public CommonResult<String> add(@RequestBody @Valid SupplierStocksAddParam supplierStocksAddParam) {
        supplierStocksService.add(supplierStocksAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑库存明细
     *
     * @author Mr.wu
     * @date  2025/03/18 14:28
     */
    @Operation(summary = "编辑库存明细")
    @CommonLog("编辑库存明细")
    @SaCheckPermission("/supplier/warehouse/stocks/edit")
    @PostMapping("/supplier/warehouse/stocks/edit")
    public CommonResult<String> edit(@RequestBody @Valid SupplierStocksEditParam supplierStocksEditParam) {
        supplierStocksService.edit(supplierStocksEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除库存明细
     *
     * @author Mr.wu
     * @date  2025/03/18 14:28
     */
    @Operation(summary = "删除库存明细")
    @CommonLog("删除库存明细")
    @SaCheckPermission("/supplier/warehouse/stocks/delete")
    @PostMapping("/supplier/warehouse/stocks/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   List<SupplierStocksIdParam> supplierStocksIdParamList) {
        supplierStocksService.delete(supplierStocksIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取库存明细详情
     *
     * @author Mr.wu
     * @date  2025/03/18 14:28
     */
    @Operation(summary = "获取库存明细详情")
    @SaCheckPermission("/supplier/warehouse/stocks/detail")
    @GetMapping("/supplier/warehouse/stocks/detail")
    public CommonResult<SupplierStocks> detail(@Valid SupplierStocksIdParam supplierStocksIdParam) {
        return CommonResult.data(supplierStocksService.detail(supplierStocksIdParam));
    }

    /**
     * 库存导出
     *
     * @author Mr.wu
     * @date 2024/11/11 20:00
     */
    @Operation(summary = "库存导出")
    @CommonLog("库存导出")
    @SaCheckPermission("/supplier/warehouse/stocks/export")
    @GetMapping(value = "/supplier/warehouse/stocks/export", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportStock(SupplierStocksExportParam supplierStocksExportParam, HttpServletResponse response) throws IOException {
        supplierStocksService.exportStock(supplierStocksExportParam, response);
    }

    /**
     * 根据供应商信息获取库存明细分页
     *
     * @author Mr.wu
     * @date  2025/05/18 14:28
     */
    @Operation(summary = "根据供应商信息获取库存明细分页")
    @SaCheckPermission("/supplier/warehouse/stocks/stocksBySupplierPage")
    @GetMapping("/supplier/warehouse/stocks/stocksBySupplierPage")
    public CommonResult<Page<SupplierStocks>> stocksBySupplierPage(SupplierProductionsPageParam supplierProductionsPageParam) {
        return CommonResult.data(supplierStocksService.stocksBySupplierPage(supplierProductionsPageParam));
    }
}
